<?php
namespace app\home\controller;

class Index extends Base {


    public $user = false;

    public function _initialize(){
        parent::_initialize();
        $this->user = $this->user();
    }

    //授权回调
    public function get_info()
    {
        $code = $_GET['code'];
        if ($code) {
            $wx_config = C('wx_config');
            $curl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $wx_config['appid'] . '&secret=' . $wx_config['appsecret'] . '&code=' . $code . '&grant_type=authorization_code';
            $res = file_get_contents($curl);//返回json数据包
            $result =  json_decode($res, true);
            session('openid',$result['openid']);
            //dump(session('openid'));
         //   pr($result);
            $url = U('User/register');
            header("Location:".$url);
        }
    }


	//非静默授权回调
    public function get_info_2()
    {
    	//file_put_contents('d.txt',print_r($_GET,true),FILE_APPEND);
        $code = $_GET['code'];
        $state = $_GET['state'];
        if ($code) {
            $wx_config = C('wx_config');
            $curl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $wx_config['appid'] . '&secret=' . $wx_config['appsecret'] . '&code=' . $code . '&grant_type=authorization_code';
            $res = file_get_contents($curl);//返回json数据包
            $result =  json_decode($res, true);
            $curl2 = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$wx_config['appid'].'&grant_type=refresh_token&refresh_token='.$result['refresh_token'];
            $res2 = file_get_contents($curl2);//返回json数据包
            $result2 =  json_decode($res2, true);
            $curl3 = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$result2['access_token'].'&openid='.$result2['openid'].'&lang=zh_CN';
            $res3 = file_get_contents($curl3);//返回json数据包
            $result3 =  json_decode($res3, true);
            $user = M('user')->where(array('openid' => $result3['openid']))->find();
            session('openid',$result3['openid']);
            if ($user){ //老用户
            	if ($state == 1) $url = U('Index/index');
            	if ($state == 2) $url = U('Goods/index');
            	if ($state == 3) $url = U('Goods/order_index');
            }else{ //新用户
            	$url = U('User/register');
            }
            header("Location:".$url);
        }
    }

    //预约-首页
    public function index(){
        if (!$this->user) $this->redirect('User/login');
        $play = [
           '球位1' =>'195,1519,377,1646',
            '球位2' =>'197,1389,377,1512',
            '球位3' =>'195,1252,379,1378',
            '球位4' =>'196,1122,381,1246',
            '球位5' =>'195,984,379,1111',
            '球位6' =>'196,1122,381,1246',
            '球位7' =>'536,1388,719,1646',
            '球位8'=>'534,1117,719,1381',
            '球位9' =>'533,847,718,1038',
            '球位10' =>'196,542,380,742',
            '球位11' =>'533,615,718,740',
            '球位12' =>'532,481,719,606',
            '球位13' =>'533,348,719,473',
            '球位14' =>'535,211,718,338',
            '球位15' =>'6,7,189,473',
        ];
            $keys = array_keys($play);
        $data = M('goods')
            ->where('goods_name','like','%球位%')
            ->field(['goods_id','goods_name','is_on_sale'])
            ->select();
        foreach ($data as $index => $value){
            if (!in_array($value['goods_name'],$keys)) continue;
            $data[$index]['location'] = $play[$value['goods_name']];
        }

        $this->assign('system',tpCache('index'));
        $this->assign('data',$data);
        $this->assign('title','首页');
        return $this->fetch();
    }


    //预约球位
    public function subscribe(){
        
        if (!$this->user) $this->redirect('User/login');
        $goods_id = I('param.itemid');
        session('coach_id',null); //预订的球位
        $detail = M('goods')->where(array('goods_id' => $goods_id))->find();
       // pr($detail);
        $detail['image'] = unserialize($detail['image']);
        //时间
        $times = $this->time_spec(); //当天的下一周
        $times2 = $this->time_spec(2); //当前的下两周
        $now = ['time' => $times[1]['start_time'],'week' => $times[1]['font']]; //当前下周的第一天时间
        //教练
        $coachs = M('coachs')->order('sort')->select();


        //推荐商品
        $category = M('goods_category')->where(['parent_id'=>2])->field(['id'])->limit(4)->select();
        $list = M('goods')
            ->whereIn('cat_id',array_column($category,'id'))
            ->where(array('is_on_sale' => 1))
            ->field([
                'goods_id',
                'original_img',
                'goods_name'
            ])
            ->order([
                'goods_id'=>'desc'
            ])
            ->select();
        $this->assign('list',$list);



        $this->assign('title','预约球位');
        $this->assign('detail',$detail);
        $this->assign('times',$times);
        $this->assign('times2',$times2);
        $this->assign('coachs',$coachs);
        $this->assign('now',$now);
        $this->user['head_img'] = head_img($this->user['head_img']);
        $this->assign('user',$this->user);
        return $this->fetch();
    }


    //预约球位时间选择
    public function ajax_subscribe(){
        $data = trim_array_element(I('param.'));

        $time = strtotime($data['time']);
        //获取球位信息
        $goods = M('goods')->where(array('goods_id' => $data['goods_id']))->find();
        $goods['vip_price'] = (float)$goods['vip_price'];

        $array = array();
        $amount = array();
        if ((date('w',$time) == 6) || (date('w',$time) == 0)){ //是周末
            $amount = array('am' => $goods['weekend_price_am'],'pm' => $goods['weekend_price_pm'],'en' => $goods['weekend_price_en'],'vip'=>$goods['vip_price']);
        }else{ //不是周末
            $amount = array('am' => $goods['weekdays_price_am'],'pm' => $goods['weekdays_price_pm'],'en' => $goods['weekdays_price_en'],'vip'=>$goods['vip_price']);
        }

        //是否有设置节假日价格
        if ($goods['season_price'] != ''){
            //获取设置的每个单日价格
            $prices = explode(',',$goods['season_price']);

            foreach ($prices as $key=>$val){
                $array[] = explode('|',$val);
            }
            if (!empty($array)){
                foreach ($array as $k=>$v){
                    if ($time == strtotime($v[0])){ //预订时间在节假日内
                        $amount = array('am' => $v[1],'pm' => $v[2],'en' => $v[3],'vip'=>$goods['vip_price']);
                    }else{
                        continue;
                    }
                }
            }
        }
        $subscribe_time1 = $data['time']." "."9:00";
        $subscribe_time2 = $data['time']." "."10:05";
        $subscribe_time3 = $data['time']." "."11:10";
        $subscribe_time4 = $data['time']." "."12:15";
        $subscribe_time5 = $data['time']." "."13:20";
        $subscribe_time6 = $data['time']." "."14:25";
        $subscribe_time7 = $data['time']." "."15:30";
        $subscribe_time8 = $data['time']." "."16:35";
        $subscribe_time9 = $data['time']." "."17:40";
        $subscribe_time10 = $data['time']." "."18:45";
        $subscribe_time11 = $data['time']." "."19:50";
        $subscribe_time12 = $data['time']." "."20:55";
        $times = array(
            1 => array('time' => strtotime($subscribe_time1),'count' => M('order')->where(array('subscribe_time' => strtotime($subscribe_time1),'goods_id' => $data['goods_id'],'order_status' => ['in',[0,1]]))->count()),
            2 => array('time' => strtotime($subscribe_time2),'count' => M('order')->where(array('subscribe_time' => strtotime($subscribe_time2),'goods_id' => $data['goods_id'],'order_status' => ['in',[0,1]]))->count()),
            3 => array('time' => strtotime($subscribe_time3),'count' => M('order')->where(array('subscribe_time' => strtotime($subscribe_time3),'goods_id' => $data['goods_id'],'order_status' => ['in',[0,1]]))->count()),
            4 => array('time' => strtotime($subscribe_time4),'count' => M('order')->where(array('subscribe_time' => strtotime($subscribe_time4),'goods_id' => $data['goods_id'],'order_status' => ['in',[0,1]]))->count()),
            5 => array('time' => strtotime($subscribe_time5),'count' => M('order')->where(array('subscribe_time' => strtotime($subscribe_time5),'goods_id' => $data['goods_id'],'order_status' => ['in',[0,1]]))->count()),
            6 => array('time' => strtotime($subscribe_time6),'count' => M('order')->where(array('subscribe_time' => strtotime($subscribe_time6),'goods_id' => $data['goods_id'],'order_status' => ['in',[0,1]]))->count()),
            7 => array('time' => strtotime($subscribe_time7),'count' => M('order')->where(array('subscribe_time' => strtotime($subscribe_time7),'goods_id' => $data['goods_id'],'order_status' => ['in',[0,1]]))->count()),
            8 => array('time' => strtotime($subscribe_time8),'count' => M('order')->where(array('subscribe_time' => strtotime($subscribe_time8),'goods_id' => $data['goods_id'],'order_status' => ['in',[0,1]]))->count()),
            9 => array('time' => strtotime($subscribe_time9),'count' => M('order')->where(array('subscribe_time' => strtotime($subscribe_time9),'goods_id' => $data['goods_id'],'order_status' => ['in',[0,1]]))->count()),
            10 => array('time' => strtotime($subscribe_time10),'count' => M('order')->where(array('subscribe_time' => strtotime($subscribe_time10),'goods_id' => $data['goods_id'],'order_status' => ['in',[0,1]]))->count()),
            11 => array('time' => strtotime($subscribe_time11),'count' => M('order')->where(array('subscribe_time' => strtotime($subscribe_time11),'goods_id' => $data['goods_id'],'order_status' => ['in',[0,1]]))->count()),
            12 => array('time' => strtotime($subscribe_time12),'count' => M('order')->where(array('subscribe_time' => strtotime($subscribe_time12),'goods_id' => $data['goods_id'],'order_status' => ['in',[0,1]]))->count()),
        );
        $this->assign('amount',$amount);
        $this->assign('now_time',time());
        $this->assign('date',$data['time']);
        $this->assign('times',$times);
        $this->assign('user',$this->user);
        return $this->fetch();
    }

    //预约信息预提交
    public function reserve(){
        if (!$this->user) ajaxReturn(array('status' => 404));
        $data = trim_array_element(I('param.'));
        session('yd_date',$data['yd_date']); //预订的日期
        session('yd_time',$data['yd_time']); //预订的时间
        session('yd_week',$data['yd_week']); //预订的时间
        session('goods_id',$data['goods_id']); //预订的球位
        session('duoxuan',$data['duoxuan']); //预订的球位
        ajaxReturn(array('status' => 200));
    }


    //时间
    public function time_spec($type = 1){
        $now = date('Y-m-d',time()); //当前时间
        $flash_sale_time = date("Y-m-d",strtotime("+0days",strtotime($now)))." 00:00:00";
        if ($type == 2){
            $flash_sale_time = date("Y-m-d",strtotime("+7days",strtotime($now)))." 00:00:00";
        }
        $time_space = array(
            '1'  =>array('time_id' => 1,'font' => '周'.$this->cnWeek(strtotime($flash_sale_time)), 'start_time' => strtotime($flash_sale_time), 'end_time' => strtotime("$flash_sale_time +1 day -1 second")),
            '2'  =>array('time_id' => 2,'font' => '周'.$this->cnWeek(strtotime("$flash_sale_time +1 day")), 'start_time' => strtotime("$flash_sale_time +1 day") , 'end_time' => strtotime("$flash_sale_time +2 day -1 second")),
            '3'  =>array('time_id' => 3,'font' => '周'.$this->cnWeek(strtotime("$flash_sale_time +2 day")), 'start_time' => strtotime("$flash_sale_time +2 day") , 'end_time' => strtotime("$flash_sale_time +3 day -1 second")),
            '4'  =>array('time_id' => 4,'font' => '周'.$this->cnWeek(strtotime("$flash_sale_time +3 day")), 'start_time' => strtotime("$flash_sale_time +3 day") , 'end_time' => strtotime("$flash_sale_time +4 day -1 second")),
            '5'  =>array('time_id' => 5,'font' => '周'.$this->cnWeek(strtotime("$flash_sale_time +4 day")), 'start_time' => strtotime("$flash_sale_time +4 day") , 'end_time' => strtotime("$flash_sale_time +5 day -1 second")),
            '6'  =>array('time_id' => 6,'font' => '周'.$this->cnWeek(strtotime("$flash_sale_time +5 day")), 'start_time' => strtotime("$flash_sale_time +5 day") , 'end_time' => strtotime("$flash_sale_time +6 day -1 second")),
            '7'  =>array('time_id' => 7,'font' => '周'.$this->cnWeek(strtotime("$flash_sale_time +6 day")), 'start_time' => strtotime("$flash_sale_time +6 day") , 'end_time' => strtotime("$flash_sale_time +7 day -1 second")),
        );
        return $time_space;
    }

    public function cnWeek($date)
    {
        $arr = array('日','一','二','三','四','五','六');
        return $arr[date('w',$date)];
    }








}